home *** CD-ROM | disk | FTP | other *** search
/ Champak 50 / Volume 50 - JOGO DISK .iso / Games / moonstonemadness.swf / scripts / __Packages / SideScroller / BasicCamera.as next >
Encoding:
Text File  |  2007-09-27  |  5.0 KB  |  174 lines

  1. class SideScroller.BasicCamera
  2. {
  3.    var nStageWidth;
  4.    var nStageHeight;
  5.    var oParent;
  6.    var nMode;
  7.    var nFollowTargetPositionX;
  8.    var nFollowTargetPositionY;
  9.    var nFollowAddonX;
  10.    var nFollowAddonY;
  11.    var nMaxSpeed;
  12.    var nAcceleration;
  13.    var nCurrentX;
  14.    var nCurrentY;
  15.    var nSpeedX;
  16.    var nSpeedY;
  17.    var bFirstStartUp;
  18.    var bPaused;
  19.    var oLockedObject;
  20.    var nTargetX;
  21.    var nTargetY;
  22.    static var MODE_LOCK = 1;
  23.    static var MODE_FREE = 2;
  24.    static var BUFFER_SCREEN_LEFT = 50;
  25.    static var BUFFER_SCREEN_TOP = 50;
  26.    static var BUFFER_SCREEN_RIGHT = 50;
  27.    static var BUFFER_SCREEN_BOTTOM = 50;
  28.    static var BUFFER_FOLLOW = 4;
  29.    static var DEFAULT_FOLLOW_CENTER_X = 150;
  30.    static var DEFAULT_FOLLOW_CENTER_Y = 150;
  31.    static var SPEED_MAX = 25;
  32.    static var ACCELERATION = 30;
  33.    function BasicCamera(__nStageWidth, __nStageHeight, __oParent)
  34.    {
  35.       this.nStageWidth = __nStageWidth;
  36.       this.nStageHeight = __nStageHeight;
  37.       this.oParent = __oParent;
  38.       this.nMode = SideScroller.BasicCamera.MODE_FREE;
  39.       this.nFollowTargetPositionX = SideScroller.BasicCamera.DEFAULT_FOLLOW_CENTER_X;
  40.       this.nFollowTargetPositionY = SideScroller.BasicCamera.DEFAULT_FOLLOW_CENTER_Y;
  41.       this.nFollowAddonX = 0;
  42.       this.nFollowAddonY = 0;
  43.       this.nMaxSpeed = SideScroller.BasicCamera.SPEED_MAX;
  44.       this.nAcceleration = SideScroller.BasicCamera.ACCELERATION;
  45.       this.nCurrentX = 0;
  46.       this.nCurrentY = 0;
  47.       this.nSpeedX = 0;
  48.       this.nSpeedY = 0;
  49.       this.bFirstStartUp = true;
  50.       this.bPaused = false;
  51.       this.oParent.doAddListener(this);
  52.    }
  53.    function doPause()
  54.    {
  55.       this.bPaused = true;
  56.    }
  57.    function doResume()
  58.    {
  59.       this.bPaused = false;
  60.    }
  61.    function setFreeMode()
  62.    {
  63.       this.nMode = SideScroller.BasicCamera.MODE_FREE;
  64.       delete this.oLockedObject;
  65.    }
  66.    function doLockOn(__oElement)
  67.    {
  68.       this.nMode = SideScroller.BasicCamera.MODE_LOCK;
  69.       this.oLockedObject = __oElement;
  70.       if(this.bFirstStartUp)
  71.       {
  72.          this.bFirstStartUp = false;
  73.          this.doMoveTo(this.oLockedObject.Ref._x,this.oLockedObject.Ref._y);
  74.       }
  75.    }
  76.    function doEnterFrame()
  77.    {
  78.       if(!this.bPaused)
  79.       {
  80.          if(this.nMode == SideScroller.BasicCamera.MODE_LOCK)
  81.          {
  82.             this.nTargetX = this.oLockedObject.Ref._x - SideScroller.BasicCamera.BUFFER_SCREEN_LEFT - this.nFollowTargetPositionX - this.nFollowAddonX;
  83.             this.nTargetY = this.oLockedObject.Ref._y - SideScroller.BasicCamera.BUFFER_SCREEN_TOP - this.nFollowTargetPositionY - this.nFollowAddonY;
  84.          }
  85.          this.nSpeedX = (this.nTargetX - this.nCurrentX) / (100 / this.nAcceleration);
  86.          this.nSpeedY = (this.nTargetY - this.nCurrentY) / (100 / this.nAcceleration);
  87.          if(Math.abs(this.nSpeedX) > this.nMaxSpeed)
  88.          {
  89.             this.nSpeedX = this.nMaxSpeed * Library.Utils.MoreMath.getPolarity(this.nSpeedX);
  90.          }
  91.          if(Math.abs(this.nSpeedY) > this.nMaxSpeed)
  92.          {
  93.             this.nSpeedY = this.nMaxSpeed * Library.Utils.MoreMath.getPolarity(this.nSpeedY);
  94.          }
  95.          if(Math.abs(this.nSpeedX) < 0.2)
  96.          {
  97.             this.nSpeedX = 0;
  98.             this.nCurrentX = this.nTargetX;
  99.          }
  100.          if(Math.abs(this.nSpeedY) < 0.2)
  101.          {
  102.             this.nSpeedY = 0;
  103.             this.nCurrentY = this.nTargetY;
  104.          }
  105.          this.doMoveTo(this.nCurrentX + this.nSpeedX,this.nCurrentY + this.nSpeedY);
  106.       }
  107.    }
  108.    function doTravelTo(__nX, __nY)
  109.    {
  110.       this.setFreeMode();
  111.       this.nTargetX = __nX;
  112.       this.nTargetY = __nY;
  113.    }
  114.    function doMoveTo(__nX, __nY)
  115.    {
  116.       if(__nX < this.oParent.Limits.left)
  117.       {
  118.          __nX = this.oParent.Limits.left;
  119.       }
  120.       if(__nX > this.oParent.Limits.right - this.nStageWidth)
  121.       {
  122.          __nX = this.oParent.Limits.right - this.nStageWidth;
  123.       }
  124.       if(__nY < this.oParent.Limits.top)
  125.       {
  126.          __nY = this.oParent.Limits.top;
  127.       }
  128.       if(__nY > this.oParent.Limits.bottom - this.nStageHeight)
  129.       {
  130.          __nY = this.oParent.Limits.bottom - this.nStageHeight;
  131.       }
  132.       this.nCurrentX = __nX;
  133.       this.nCurrentY = __nY;
  134.    }
  135.    function doDestroy()
  136.    {
  137.       this.oParent.doRemoveListener(this);
  138.       delete this.oParent;
  139.       delete this.oLockedObject;
  140.    }
  141.    function get PosX()
  142.    {
  143.       return - this.nCurrentX;
  144.    }
  145.    function get PosY()
  146.    {
  147.       return - this.nCurrentY;
  148.    }
  149.    function set MaxSpeed(__nSpeed)
  150.    {
  151.       this.nMaxSpeed = __nSpeed;
  152.    }
  153.    function set Acceleration(__nAcceleration)
  154.    {
  155.       this.nAcceleration = __nAcceleration;
  156.    }
  157.    function set FollowAddonX(__nFollowAddonX)
  158.    {
  159.       this.nFollowAddonX = __nFollowAddonX;
  160.    }
  161.    function get FollowAddonX()
  162.    {
  163.       return this.nFollowAddonX;
  164.    }
  165.    function set FollowAddonY(__nFollowAddonY)
  166.    {
  167.       this.nFollowAddonY = __nFollowAddonY;
  168.    }
  169.    function get FollowAddonY()
  170.    {
  171.       return this.nFollowAddonY;
  172.    }
  173. }
  174.